﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Party.PartyComponents.LordPartyComponent
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.CampaignSystem.Extensions;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Party.PartyComponents
{
  public class LordPartyComponent : WarPartyComponent
  {
    [CachedData]
    private TextObject _cachedName;
    [SaveableField(30)]
    private Hero _leader;
    [SaveableField(40)]
    private int _wagePaymentLimit = Campaign.Current.Models.PartyWageModel.MaxWage;

    internal static void AutoGeneratedStaticCollectObjectsLordPartyComponent(
      object o,
      List<object> collectedObjects)
    {
      ((PartyComponent) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this._leader);
      collectedObjects.Add((object) this.Owner);
    }

    internal static object AutoGeneratedGetMemberValueOwner(object o)
    {
      return (object) ((LordPartyComponent) o).Owner;
    }

    internal static object AutoGeneratedGetMemberValue_leader(object o)
    {
      return (object) ((LordPartyComponent) o)._leader;
    }

    internal static object AutoGeneratedGetMemberValue_wagePaymentLimit(object o)
    {
      return (object) ((LordPartyComponent) o)._wagePaymentLimit;
    }

    public override Hero PartyOwner => this.Owner;

    public override TextObject Name
    {
      get
      {
        if (this._cachedName == null && this.Owner != null)
          this._cachedName = this.GetPartyName();
        return this._cachedName ?? new TextObject("{=!}unnamedMobileParty");
      }
    }

    public override Settlement HomeSettlement => this.Owner.HomeSettlement;

    [SaveableProperty(20)]
    public Hero Owner { get; private set; }

    public override Hero Leader => this._leader;

    public override int WagePaymentLimit => this._wagePaymentLimit;

    public override void SetWagePaymentLimit(int newLimit) => this._wagePaymentLimit = newLimit;

    public static MobileParty CreateLordParty(
      string stringId,
      Hero hero,
      Vec2 position,
      float spawnRadius,
      Settlement spawnSettlement,
      Hero partyLeader)
    {
      return MobileParty.CreateParty(hero.CharacterObject.StringId + "_party_1", (PartyComponent) new LordPartyComponent(hero, partyLeader), (PartyComponent.OnPartyComponentCreatedDelegate) (mobileParty => mobileParty.LordPartyComponent.InitializeLordPartyProperties(mobileParty, position, spawnRadius, spawnSettlement)));
    }

    protected internal LordPartyComponent(Hero owner, Hero leader)
    {
      this.Owner = owner;
      this._leader = leader;
    }

    internal void ChangePartyOwner(Hero owner)
    {
      this.ClearCachedName();
      this.Owner = owner;
    }

    public override void ChangePartyLeader(Hero newLeader)
    {
      this._leader = newLeader;
      this.ClearCachedName();
    }

    public override void ClearCachedName() => this._cachedName = (TextObject) null;

    private TextObject GetPartyName()
    {
      TextObject text = GameTexts.FindText("str_lord_party_name");
      text.SetCharacterProperties("TROOP", this.Owner.CharacterObject);
      text.SetTextVariable("IS_LORDPARTY", 1);
      return text;
    }

    private void InitializeLordPartyProperties(
      MobileParty mobileParty,
      Vec2 position,
      float spawnRadius,
      Settlement spawnSettlement)
    {
      mobileParty.AddElementToMemberRoster(this.Owner.CharacterObject, 1, true);
      mobileParty.ActualClan = this.Owner.Clan;
      int troopNumberLimit = this.Owner == Hero.MainHero || this.Owner.Clan == Clan.PlayerClan ? 0 : (int) MathF.Min(this.Owner.Clan.IsRebelClan ? 40f : 19f, (this.Owner.Clan.IsRebelClan ? 0.2f : 0.1f) * (float) mobileParty.Party.PartySizeLimit);
      if (!Campaign.Current.GameStarted)
      {
        float num = (float) (1.0 - (double) MBRandom.RandomFloat * (double) MathF.Sqrt(MBRandom.RandomFloat));
        troopNumberLimit = (int) ((double) mobileParty.Party.PartySizeLimit * (double) num);
      }
      mobileParty.InitializeMobilePartyAroundPosition(this.Owner.Clan.DefaultPartyTemplate, position, spawnRadius, troopNumberLimit: troopNumberLimit);
      mobileParty.Party.SetVisualAsDirty();
      if (spawnSettlement != null)
        mobileParty.Ai.SetMoveGoToSettlement(spawnSettlement);
      mobileParty.Aggressiveness = (float) (0.89999997615814209 + 0.10000000149011612 * (double) this.Owner.GetTraitLevel(DefaultTraits.Valor) - 0.05000000074505806 * (double) this.Owner.GetTraitLevel(DefaultTraits.Mercy));
      mobileParty.ItemRoster.Add(new ItemRosterElement(DefaultItems.Grain, MBRandom.RandomInt(15, 30)));
      this.Owner.PassedTimeAtHomeSettlement = (float) (int) ((double) MBRandom.RandomFloat * 100.0);
      if (spawnSettlement == null)
        return;
      mobileParty.Ai.SetMoveGoToSettlement(spawnSettlement);
    }
  }
}
